import { Controller } from "../../mvc/core/Controller";
import { IChangeHeadIconModule } from "../../../modules/interfaces/IChangeHeadIconModule";
import { ChangeHeadIconModule } from "../../../modules/ChangeHeadIconModule";
import { Cache } from "../cache/Cache";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { GameConst } from "../../component/gconst/GameConst";
import { SSeqAttributeUpdate } from "../../../Message/Public/SSeqAttributeUpdate";
type int = number;
//class ChangeHeadIconController
    
    export  class ChangeHeadIconController extends Controller
    {
       

        public get changeHeadIcon(): IChangeHeadIconModule
        {
            if (!this._changeHeadIcon) 
            {
                this._changeHeadIcon = new ChangeHeadIconModule();
                this._changeHeadIcon.updatePlayerBase(Cache.instance.role.entityInfo);
                this._changeHeadIcon.addEventListener(WindowEvent.SHOW, this.onChangeHeadIconWindowShow);
                this._changeHeadIcon.addEventListener(WindowEvent.CLOSE, this.onChangeHeadIconWindowClose);
                _view = this._changeHeadIcon;
            }
            return this._changeHeadIcon;
        }

        /* internal  */onChangeHeadIconWindowShow(arg1: WindowEvent): void
        {
            return;
        }

        /* internal  */onChangeHeadIconWindowClose(arg1: WindowEvent): void
        {
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ChangeHeadIconComment, this.onChangeHeadIconComment);
            Dispatcher.addEventListener(EventName.OpenChangeHeadIconWindow, this.openChangeHeadIconWindow);
            Dispatcher.addEventListener(EventName.ChangeChangeHeadIconCount, this.onChangeChangeHeadIconCount);
            Dispatcher.addEventListener(EventName.ChangePrwHead, this.onChangePrwHead);
            Dispatcher.addEventListener(EventName.ChangeHeadIconGoReplace, this.onChangeHeadIconGoReplace);
            Dispatcher.addEventListener(EventName.SexAttributeSexChange, this.onSexAttributeSexChangeHandler);
            Dispatcher.addEventListener(EventName.ChangeHeadIconGetInfo, this.onChangeHeadIconGetInfoHandler);
            return;
        }

        /* internal  */onSexAttributeSexChangeHandler(arg1: DataEvent): void
        {
            this.changeHeadIcon.preViewHeadIcon(Cache.instance.role.avatar, true);
            this.changeHeadIcon.updateInfo(Cache.instance.role.headIconArr);
            this.changeHeadIcon.updateAttributeTips();
            return;
        }

        /* internal  */onChangeHeadIconGoReplace(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.replaceAvatar(loc1);
            return;
        }

        /* internal  */onChangePrwHead(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.changeHeadIcon.preViewHeadIcon(loc1);
            return;
        }

        /* internal  */onChangeChangeHeadIconCount(arg1: DataEvent): void
        {
            var loc1=GameConst.UpdateAvatarCount - Cache.instance.role.changeHeadIconCount;
            if (loc1 < 0) 
            {
                loc1 = 0;
            }
            this.changeHeadIcon.updateChangeCount(loc1);
            return;
        }

        /* internal  */onChangeHeadIconComment(arg1: SSeqAttributeUpdate): void
        {
            var loc4=null;
            var loc1=arg1;
            var loc2=loc1.updates;
            var loc3=[];
            var loc5=0;
            while (loc5 < loc2.length) 
            {
                loc4 = loc2[loc5];
                loc3.push(loc4.value);
                ++loc5;
            }
            Cache.instance.role.avatar = loc1.code;
            Cache.instance.role.headIconArr = loc3;
            if (isViewShow) 
            {
                this.changeHeadIcon.preViewHeadIcon(loc1.code, true);
                this.changeHeadIcon.updateInfo(loc3);
                this.changeHeadIcon.updateAttributeTips();
            }
            return;
        }

        /* internal  */openChangeHeadIconWindow(arg1: DataEvent): void
        {
            var loc1=null;
            if (arg1.data) 
            {
                loc1 = arg1.data as string;
            }
            else 
            {
                loc1 = "";
            }
            GameProxy.roleProxy.getAvatarInfo(loc1);
            this.changeHeadIcon.show();
            return;
        }

        /* internal  */onChangeHeadIconGetInfoHandler(arg1: DataEvent): void
        {
            GameProxy.roleProxy.getAvatarInfo();
            return;
        }

        private /* var */_changeHeadIcon: IChangeHeadIconModule;
    }
